package spring.cloud.tasks.tasks_manager.tasks_manager.task_item_allot.task;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_manager.tasks_manager.service.AllotTaskItemService;
import spring.cloud.tasks.tasks_manager.tasks_manager.task_item_allot.TaskItemAllotTask;
import spring.cloud.tasks.tasks_manager.tasks_manager.task_item_allot.executor.Executor;

import java.util.List;

/**
 * 作业禁用，摘取所有executor运行的该作业的shard，注意要相应地减loadLevel，不需要放回
 */
@Slf4j
@Setter
@Getter
public class TaskDisableTaskItemAllotTask extends TaskItemAllotTask {
    private String taskId;

    public TaskDisableTaskItemAllotTask(AllotTaskItemService allotTaskItemService, String taskId) {
        super(allotTaskItemService);
        this.taskId = taskId;
    }

    @Override
    protected void logStartInfo() {
        log.info("Execute the {} with {} disable", this.getClass().getSimpleName(), taskId);
    }

    /**
     * 摘取后不放回
     *
     * @param taskIdList                   该域下所有作业
     * @param enableTaskIdList             该域下所有启用的作业
     * @param taskItemList                 默认为空集合
     * @param onlineExecutorList           默认为当前存储的数据，如果不想使用存储数据，请重写{@link #customLastOnlineExecutorList()}}方法
     * @param onlineOwnTrafficExecutorList lastOnlineExecutorList中所有noTraffic为false的Executor，注意Executor是同一个对象
     * @return
     */
    @Override
    protected boolean pick(List<String> taskIdList,//
                           List<String> enableTaskIdList,//
                           List<TaskItem> taskItemList,//
                           List<Executor> onlineExecutorList,//
                           List<Executor> onlineOwnTrafficExecutorList) {
        //先移除清理
        taskItemList.addAll(removeTaskItemList(onlineOwnTrafficExecutorList, taskId));
        return !taskItemList.isEmpty();
    }

    @Override
    protected void putBack(List<String> enableTaskIdList,//
                           List<TaskItem> taskItemList,//
                           List<Executor> onlineExecutorList,//
                           List<Executor> onlineOwnTrafficExecutorList) {//
        //摘取后不放回
    }

}
